package com.settlement.system.service.kafka.consumer;

import cn.hutool.json.JSONUtil;
import com.settlement.system.common.enums.AccountStatusEnum;
import com.settlement.system.common.enums.HeadProcessEnum;
import com.settlement.system.common.enums.NcAccountStatusEnum;
import com.settlement.system.common.enums.StandardStatusEnum;
import com.settlement.system.common.util.common.EnumUtil;
import com.settlement.system.model.dto.FspResultDto;
import com.settlement.system.model.dto.FspVoucherNoData;
import com.settlement.system.model.entity.common.TblNcVurcherDetailPlus;
import com.settlement.system.service.ITblNcVurcherDetailPlusService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;


/**
 * 推送NC凭证 kafka 回调
 *
 * @author hc
 * @since 2022/04/09
 */
@Slf4j
@Service
public class PushNcKafkaConsumer {

    @Value("${spring.kafka.topic.push-nc}")
    public String pushNcTopic;

    @Resource
    private ITblNcVurcherDetailPlusService tblNcVurcherDetailPlusService;

    @KafkaListener(topics = "${spring.kafka.topic.nc-result}", groupId = "${spring.kafka.customer.group-id}")
    public void callback(String message) throws Exception {
        log.info("推送NC凭证返回结果，Kafka收到消息:" + message);
        FspResultDto resultDto = JSONUtil.toBean(message, FspResultDto.class);
        log.info("kafka消费fsp返回的推送NC凭证结果消息---start，流水号serialNo={}", resultDto.getSerial_no());
        List<FspVoucherNoData> data = resultDto.getData();
        log.info("解析kafka消息成功,流水号serialNo={},凭证号生成信息：{}", resultDto.getSerial_no(), JSONUtil.toJsonStr(data));
        if (EnumUtil.eq(resultDto.getCode(), StandardStatusEnum.MULTI_OK)) {
            log.info("推送NC凭证成功,流水号serialNo={}", resultDto.getSerial_no());
            data.forEach(d -> {
                String orderNo = d.getOrder_no();
                if (EnumUtil.eq(d.getStatus(), HeadProcessEnum.HEAD_SUCCESS)) {
                    TblNcVurcherDetailPlus vurcherDetail = new TblNcVurcherDetailPlus();
                    vurcherDetail.setOrderNo(orderNo);
                    vurcherDetail.setAccountStatus(NcAccountStatusEnum.PUSH_SUCC.code());
                    vurcherDetail.setPkVurcher(d.getPkVoucher());
                    vurcherDetail.setUpdateTime(new Date());
                    vurcherDetail.setFailDesc("");
                    tblNcVurcherDetailPlusService.updateByOrderNo(vurcherDetail);
                }
            });
            tblNcVurcherDetailPlusService.updateNcVoucherNoBySerialNo(data.get(0).getVoucher_no(),resultDto.getSerial_no());
            tblNcVurcherDetailPlusService.updateAccountStatusOfCostAllocationBySerialNo(resultDto.getSerial_no(), AccountStatusEnum.T5_HAS_PUSHED_NC.getNo(),"");
            tblNcVurcherDetailPlusService.updateAccountStatusOfUnificationSummaryBySerialNo(resultDto.getSerial_no(),AccountStatusEnum.T5_HAS_PUSHED_NC.getNo(),"");
        } else {
            log.info("推送NC凭证失败,流水号serialNo={}", resultDto.getSerial_no());
            TblNcVurcherDetailPlus vurcherDetail = new TblNcVurcherDetailPlus();
            vurcherDetail.setSerialNo(resultDto.getSerial_no());
            vurcherDetail.setAccountStatus(NcAccountStatusEnum.PUSH_FAIL.code());
            vurcherDetail.setFailDesc(resultDto.getMessage());
            vurcherDetail.setPkVurcher("clean");
            tblNcVurcherDetailPlusService.updateBySerialNo(vurcherDetail);
            tblNcVurcherDetailPlusService.updateAccountStatusOfCostAllocationBySerialNo(resultDto.getSerial_no(),AccountStatusEnum.T6_PUSH_NC_FAILED.getNo(), StringUtils.substring(resultDto.getMessage(),0,500));
            tblNcVurcherDetailPlusService.updateAccountStatusOfUnificationSummaryBySerialNo(resultDto.getSerial_no(),AccountStatusEnum.T6_PUSH_NC_FAILED.getNo(),StringUtils.substring(resultDto.getMessage(),0,500));
        }
        log.info("kafka消费fsp返回的推送NC凭证结果消息---end，流水号serialNo={}", resultDto.getSerial_no());
    }

}
